package com.yztc.thinkpad.mandemo.single;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.yztc.thinkpad.mandemo.R;
import com.yztc.thinkpad.mandemo.single.bean.CategoriesBean;
import com.yztc.thinkpad.mandemo.single.bean.ContentBean;
import com.yztc.thinkpad.mandemo.single.bean.DataBean;

import java.util.ArrayList;
import java.util.List;

import homewebview.VpWebView;
import utils.MyGridView;

/**
 * Created by Administrator on 2016/10/8.
 */
public class SingleRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;//上下文
    private static final int TYPE_ONE =0;//GridView
    private static final int TYPE_TWO = 1; //textview
    private static final int TYPE_THREE=2;//recycleview;
    private static final int TYPE_FOUR=3;//下拉加载更多;
    //上拉加载更多状态-默认为0
    public static int load_more_status=0;
    private DataBean type;
    private List<CategoriesBean> typelist= new ArrayList<>();
    private List<ContentBean.DataBean> datalist;
    public SingleRecyclerAdapter(Context context, DataBean type, List<ContentBean.DataBean> datalist){
        this.context=context;
        this.type=type;
        this.datalist=datalist;
        typelist = type.getCategories();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //进行判断显示类型，来创建返回不同的View
        if(viewType==TYPE_ONE){
            GridViewHolder gridholder=new GridViewHolder(LayoutInflater.from(context)
                    .inflate(R.layout.single_viewpager_gridview,parent,false));
            return gridholder;
        }else if(viewType==TYPE_TWO){
            TextViewHolder textholder=new TextViewHolder(LayoutInflater.from(context)
                    .inflate(R.layout.single_viewpager_textview,parent,false));
            return textholder;
        }else if(viewType==TYPE_THREE){
            MyViewHolder myholder=new MyViewHolder(LayoutInflater.from(context)
                    .inflate(R.layout.item_recycle_single,parent,false));
            return myholder;
        }else if(viewType==TYPE_FOUR){
            FootViewHolder footholder=new FootViewHolder(LayoutInflater.from(context)
                    .inflate(R.layout.recycler_load_more_layout,parent,false));
            return footholder;
        }
        return null;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        //点击item跳转到CoolWebViewActivity
//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent(context, CoolWebViewActivity.class);
//                intent.putExtra("title", list.get(position).getTitle());
//                intent.putExtra("id", list.get(position).getDress_school_id() + "");
//                intent.putExtra("image",list.get(position).getImage());
//                context.startActivity(intent);
//            }
//        });
        //item的布局控件
        if(holder instanceof MyViewHolder) {
            ((MyViewHolder)holder).title_centnt.setText(datalist.get(position-2).getTitle());
            ((MyViewHolder)holder).price_centent.setText(datalist.get(position-2).getCoupon_price());
            Glide.with(context).load(datalist.get(position-2).getPic_url()).into( ((MyViewHolder)holder).image_centent);
        }else if(holder instanceof FootViewHolder){//foot
            switch (load_more_status){
                case 0:
                    ((FootViewHolder) holder).tv_jiazai.setText("上拉加载更多...");
                    Glide.with(context).load(R.mipmap.jiazai).into(((FootViewHolder) holder).iv_jiazai);
                    break;
                case 1:
                    ((FootViewHolder) holder).tv_jiazai.setText("没有更多数据啦...");
                    Glide.with(context).load(R.mipmap.ok).into(((FootViewHolder) holder).iv_jiazai);
                    break;
            }

        }else if(holder instanceof GridViewHolder){//grid
            SingleTypeAdapter adapter = new SingleTypeAdapter(context,typelist);
            ( (GridViewHolder)holder).mygridview_home_type.setAdapter(adapter);
        }else if(holder instanceof  TextViewHolder){//text
            ((TextViewHolder)holder).title_single.setText(type.getName());
        }
    }
    //注意事项：list.size()+1
    @Override
    public int getItemCount() {
        return datalist.size()+1;
    }
    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_ONE;//Grid布局
        }
        if (position == 1) {
            return TYPE_TWO;//text布局
        }
//        if (position == 2) {
//            return TYPE_THREE;//recycleview布局
//        }
        if (position + 1 == getItemCount()) {
            return TYPE_FOUR;//foot布局
        }
        else {
            return TYPE_THREE;
        }
    }
    //recycleview布局
    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView title_centnt,price_centent;
        ImageView image_centent;

        public MyViewHolder(View itemView) {
            super(itemView);
            image_centent= (ImageView)itemView.findViewById(R.id.image_centent);
            title_centnt= (TextView) itemView.findViewById(R.id.title_centnt);
            price_centent= (TextView) itemView.findViewById(R.id.price_centent);
        }
    }
    //text布局
    class TextViewHolder extends RecyclerView.ViewHolder{
        TextView title_single;
        public TextViewHolder(View itemView) {
            super(itemView);
            title_single= (TextView)itemView.findViewById(R.id.title_single);

        }
    }
    //下拉加载更多布局
    class FootViewHolder extends RecyclerView.ViewHolder{
        TextView tv_jiazai;
        ImageView iv_jiazai;
        public FootViewHolder(View itemView) {
            super(itemView);
            tv_jiazai= (TextView)itemView.findViewById(R.id.tv_jiazai);
            iv_jiazai= (ImageView)itemView.findViewById(R.id.iv_jiazai);
        }
    }
    //Grid布局
    class GridViewHolder extends RecyclerView.ViewHolder{
    MyGridView mygridview_home_type;
        public GridViewHolder(View itemView) {
            super(itemView);
            mygridview_home_type= (MyGridView) itemView.findViewById(R.id.mygridview_home_type);
            mygridview_home_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> view, View view1, int i, long l) {
                    Toast.makeText(context,"hhhhhhhh",Toast.LENGTH_SHORT).show();
                   /* String url = typelist.get(i).getCateimg();
                    String theme_name=mListBean.get(position).getTheme_name();
                    Intent intent=new Intent(getActivity(), VpWebView.class);
                    intent.putExtra("url",url);
                    intent.putExtra("theme_name",theme_name);
                    startActivity(intent);*/
                }
            });
        }
    }
    //分页添加到list集合
    public void rush(List<ContentBean.DataBean>mlist, boolean isClear){
        if(isClear){
            datalist.clear();
        }
        datalist.addAll(mlist);
        notifyDataSetChanged();
    }
}
